package com.manor.common.utils;

import android.app.Activity;
import android.content.Context;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.manor.common.R;
import com.manor.common.activity.AbsActivity;
import com.manor.common.interfaces.CommonCallback;
import com.manor.common.upload.UploadBean;
import com.manor.common.upload.UploadCallback;
import com.manor.common.upload.UploadQnImpl;
import com.manor.common.upload.UploadStrategy;


/**

 * @version 1.0

 * @date 2020/7/27  16:47
 */
public class AuthUtil {
    // private UploadStrategy mUploadStrategy;
    private UploadStrategy uploadStrategy;
    public static AuthUtil instance = null;

    static {
        if (instance == null) {
            instance = new AuthUtil();
        }
    }

    public void startCamera(final Context context, final CommonCallback<Boolean> callback){
            AndPermission.with(context)
                    .runtime()
                    .permission(Permission.CAMERA)//相机
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            callback.callback(true);
                        }
                    })
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            ToastUtil.show(context.getString(R.string.permissions_camera));
                            if (AndPermission.hasAlwaysDeniedPermission(context, data)) {
                                ((AbsActivity) context).showSettingDialog(context,
                                        context.getString(R.string.permissions_camera),12);
                            } else {
                                startCamera(context,callback);
                            }
                        }
                    })
                    .start();

    }


//    public void startAuth(final Context context, final CommonCallback<Boolean> callback) {
//        String metaInfo = ZIMFacade.getMetaInfos(context);
//        JCommonHttpUtil.getAuthToken(metaInfo, new JHttpCallback() {
//            @Override
//            public void onSuccess(int code, String msg, String info) {
//                if (code == 200 && StringUtil.isNotEmpty(info)) {
//                    JSONObject obj = JSON.parseObject(info);
//                    JSONObject o = obj.getJSONObject("resultObject");
//                    String token = o.getString("certifyId");
//
//                    ZIMFacade zimFacade = ZIMFacadeBuilder.create(context);
//
//                    HashMap<String, String> extParams = new HashMap<>();
//                    //如需指定活体检测UI界面方向(横屏+竖屏)，请指定这一项
//                    extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_SCREEN_ORIENTATION,
//                            ZIMFacade.ZIM_EXT_PARAMS_VAL_SCREEN_PORT);
//                    //如需支持活体视频返回，请指定这一项，并在response.videoFilePath中获取视频本地路径
//                    //extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_USE_VIDEO, ZIMFacade
//                    // .ZIM_EXT_PARAMS_VAL_USE_VIDEO_TRUE);
//
//                    //如需设置OCR的“下一步”按钮颜色（默认可不设置），请设置此项，如红色 #FF0000
//                    extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_OCR_BOTTOM_BUTTON_COLOR, "#FF0000");
//                    //如需自定义活体检测页面的进度条颜色（默认可不设置），请设置此项，如红色 #FF0000
//                    extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_FACE_PROGRESS_COLOR, "#FF0000");
//                    zimFacade.verify(token, true,extParams, new ZIMCallback() {
//                        @Override
//                        public boolean response(ZIMResponse response) {
//                            if (null != response && 1000 == response.code) {
//                                // 认证成功
//                                if (callback != null)
//                                    callback.callback(true);
//                            } else {
//                                // 认证失败
//                                if (callback != null)
//                                    callback.callback(false);
//                            }
//                            return true;
//                        }
//                    });
//
//
//                } else {
//                    ToastUtil.show(msg);
//                }
//            }
//        });
//
//
//    }

    public void uploadImage(Activity mContext, File upFile,
                            final CommonCallback<String> commonCallback) {
        if (uploadStrategy == null) {
            uploadStrategy = new UploadQnImpl(mContext);
        }
        List<UploadBean> list = new ArrayList<>();
        list.add(new UploadBean(upFile));
        uploadStrategy.upload(list, true, new UploadCallback() {
            @Override
            public void onFinish(List<UploadBean> list, boolean success) {
                if (success) {
                    if (list != null && list.size() > 0) {
                        String url = list.get(0).getRemoteFileName();
                        KLog.e("uploadImage-onFinish", url);
                        if (StringUtil.isNotEmpty(url) && commonCallback != null) {
                            commonCallback.callback(url);

                        }
                    }
                }
            }


        });
    }

}
